package com.example.user.contrller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.constants.UserConstants;
import com.example.common.util.R;
import com.example.common.util.ResultCodeRecord;
import com.example.model.user.dto.AuthDto;
import com.example.model.user.pojo.UserRealname;
import com.example.user.service.UserRealnameService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "实名认证管理")
@RestController
@RequestMapping(value = "/api/v1/")
public class UserRealnameController {
    @Autowired
    private UserRealnameService userRealnameService;

    @GetMapping(value = "/auth/getUserRealNamePage/{page}/{pageSize}")
    @Operation(summary = "查询模糊实名认证分页")
    public R getUserRealNamePage(@RequestBody AuthDto authDto, @PathVariable(value = "page") Long page, @PathVariable(value = "pageSize") Long pageSize) {
        // 1、 检查参数是否有效
        if (authDto == null) {
            return R.error(ResultCodeRecord.PARAM_INVALID);
        }
        // 2、分页查询
        Page< UserRealname > userRealnamePage = new Page<>(page, pageSize);
        // 3、条件构造器
        LambdaQueryWrapper< UserRealname > wrapper = new LambdaQueryWrapper<>();
        // 模糊敏感词查询
        if (authDto.getUserStatus() != null) {
            wrapper.like(UserRealname::getUserStatus, authDto.getUserStatus());
        }
        // 查询数据
        userRealnameService.page(userRealnamePage, wrapper);
        // 每页的数据集合
        List< UserRealname > userRealnameList = userRealnamePage.getRecords();
        // 总的记录数
        Long total = userRealnamePage.getTotal();
        //每页条数
        Long size = userRealnamePage.getSize();
        // 总页数
        Long pages = userRealnamePage.getPages();
        Map< String, Object > map = new HashMap<>();
        map.put("total", total);
        map.put("pageSize", size);
        map.put("pages", pages);
        map.put("page", page);
        map.put("userRealnameList", userRealnameList);
        if (map != null) {
            return R.ok("查询模糊实名认证分页成功").data(map);
        } else {
            return R.error("查询模糊实名认证分页失败");
        }
    }

    @PostMapping(value = "/auth/auth_pass")
    @Operation(summary = "实名认证通过")
    public R authPass(@RequestBody AuthDto authDto) {
        boolean flag = userRealnameService.updateUserStatus(authDto, UserConstants.PASS_AUTH);
        if (flag) {
            return R.ok("实名认证通过成功");
        } else {
            return R.error("实名认证通过失败");
        }
    }

    @PostMapping(value = "/auth/auth_fail")
    @Operation(summary = "实名认证拒绝")
    public R authFail(@RequestBody AuthDto authDto) {
        boolean flag = userRealnameService.updateUserStatus(authDto, UserConstants.FAIL_AUTH);
        if (flag) {
            return R.ok("实名认证拒绝成功");
        } else {
            return R.error("实名认证拒绝失败");
        }
    }
}
